package cn.edu.gues.infosystam.service.impl;

import cn.edu.gues.infosystam.entity.Student;
import cn.edu.gues.infosystam.entity.Task;
import cn.edu.gues.infosystam.mapper.TaskMapper;
import cn.edu.gues.infosystam.service.StudentService;
import cn.edu.gues.infosystam.service.TaskService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苏小小
 * @since 2020-06-11
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private StudentService studentService;

    @Override
    public IPage<Task> getTaskList(Page<Task> page, QueryWrapper<Task> queryWrapper) {
        return this.baseMapper.getTaskList(page, queryWrapper);
    }

    @Override
    public IPage<Student> getTaskDetail(Page<Student> page, Integer taskId) {
        Task task = this.baseMapper.selectById(taskId);
        if (task == null)
            return null;
        // 定义学院和班级
        String college = "", classes = "";
        String range = task.getTaskRange();
//        System.out.println("填报范围："+range);
        if (!range.equals("\"all\"")) {
            JSONArray object = JSON.parseArray(range);
            for (Object o : object) {
                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                String letCollege = jsonObject.getString("college");    // 定义临时学院变量
                String letValue = jsonObject.getString("value");        // 定义临时班级数组变量

                //            System.out.println("\n\n学院：" +letCollege + "   ====>  班级："+ letValue);

                if (letValue.equals("all")) {
                    college = String.join("|", college, letCollege);
                } else {
                    JSONArray objects = JSONArray.parseArray(letValue);
                    for (Object value : objects) {
                        //                    System.out.println("班级："+value);
                        classes = String.join("|", classes, value.toString());
                    }
                }
            }

            //        System.out.println("学院："+college);
            //        System.out.println("班级："+classes);
            // 去除前面多出的一个 | 符号
            if (college.length() > 1)
                college = college.substring(1);
            else college = "x";

            if (classes.length() > 1)
                classes = classes.substring(1);
            else classes = "x";

            // 当不是选择全校时使用
            return studentService.getReportStudents(page, taskId, college, classes);

        }else {
            // 全校时使用
            return studentService.getReportTaskStudents(page, taskId);
        }
    }

    @Override
    public Integer getTaskDetailReportTotal(Integer taskId) {
        Task task = this.baseMapper.selectById(taskId);
        // 定义学院和班级
        String college = "", classes = "";
        String range = task.getTaskRange();

        if (!range.equals("\"all\"")) {

            Map<String, String> range1 = getRange(range);
            college = range1.get("college");
            classes = range1.get("classes");

            System.out.println("学院："+college);
            System.out.println("班级："+classes);
            // 当不是选择全校时使用
            return studentService.getReportTaskStudentsTotal(taskId, college, classes);
        }else {
            return studentService.count();
        }

    }

    private Map<String, String> getRange(String range){
        // 定义学院和班级
        String college = "", classes = "";

        JSONArray object = JSON.parseArray(range);
        for (Object o : object) {
            JSONObject jsonObject = JSONObject.parseObject(o.toString());
            String letCollege = jsonObject.getString("college");    // 定义临时学院变量
            String letValue = jsonObject.getString("value");        // 定义临时班级数组变量

            if (letValue.equals("all")) {
                college = String.join("|", college, letCollege);
            } else {
                JSONArray objects = JSONArray.parseArray(letValue);
                for (Object value : objects) {
                    //                    System.out.println("班级："+value);
                    classes = String.join("|", classes, value.toString());
                }
            }
        }

        // 去除前面多出的一个 | 符号
        if (college.length() > 1)
            college = college.substring(1);
        else college = "x";

        if (classes.length() > 1)
            classes = classes.substring(1);
        else classes = "x";

        Map<String, String> m = new HashMap<>(3);
        m.put("college", college);
        m.put("classes", classes);
        return m;
    }
}
